<template>
	<view class="raffleList">
		<z-paging ref="paging" v-model="dataList" @query="queryList" :default-page-size="15">
			<template #loading>
				<uni-load-more status="loading"></uni-load-more>
			</template>
			<uni-list  border-full v-if="dataList.length">			
				<uni-list-item clickable 
				@click="routerTo(`/pages_raffle/detail/detail?id=${item._id}`)" 
				showArrow  :title="item.nickname+'-创建的投票'" 
				:note="`创建于：${dayjs(item.create_date).format('YYYY-MM-DD HH:mm:ss')}\n已参与:${item.join_count}人`" 				
				:rightText="activeStateTxt(item.active_state)" 				
				v-for="item in dataList" 
				:key="item._id"/>
				
			</uni-list>
		</z-paging>
		
		<uni-fab ref="fab" :pattern="{icon:'plusempty'}" :content="content" horizontal="right" vertical="bottom"
		@fabClick="addRaffle" />
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { routerTo ,activeStateTxt, showToast} from '../../utils/common';
import dayjs from "dayjs"

const db = uniCloud.database();
const paging = ref(null);
const dataList = ref([]);

const queryList =async (pageNo, pageSize)=>{
	console.log(pageNo, pageSize);	
	let raffleTemp = db.collection("raffle-data").orderBy("create_date desc").getTemp();
	let userTemp = db.collection("uni-id-users").field("_id,nickname").getTemp();
	
	try{
		let {result:{data,errCode}} = await db.collection(raffleTemp,userTemp)
		.field("active_state,create_date,join_count,arrayElemAt(user_id.nickname,0) as nickname,_id")
		.get();
		if(errCode==0){
			paging.value.complete(data);
			console.log(data);
		}else{
			paging.value.complete(false);
		}
	}catch(err){
		showToast(err)
	}
	
}

const addRaffle = ()=>{	
	routerTo("/pages_raffle/edit/edit")
}


</script>

<style lang="scss" scoped>

</style>
